package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.bean.AddPreOrderResVoBean;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.popwindow.BasicPopmodule;

import java.text.DecimalFormat;

/**
 * 定金
 */
public class PopPayModule extends BasicPopmodule {

    private Context mContext;
    private PopPayListener mPoplistener;
    private double mDeposit; //需要支付的金额
    private double mPaybalance; //清洁宝余额总数
    private String mStarts;
    private String mTitleName;//布局有2个地方用到，标题名称动态修改
    private AddPreOrderResVoBean mAddpreOrderResVo;
    private String mAlipayOrWeixin;

    public PopPayModule(Context context, double deposit, double paybalance,String mStarts,String TitleName,AddPreOrderResVoBean mAddpreOrderResVo, PopPayListener l) {
        this.mContext = context;
        this.mPoplistener = l;
        this.mDeposit = deposit;
        this.mPaybalance = paybalance;
        this.mStarts=mStarts;
        this.mTitleName=TitleName;
        this.mAddpreOrderResVo = mAddpreOrderResVo;
        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_pay_module, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener() {
        View mVwPopBlank = (View) mContentView.findViewById(R.id.vw_pop_blank);
        TextView mTvOrderStartsCancelEvaluate = (TextView) mContentView.findViewById(R.id.tv_order_starts_cancel_evaluate);
        final TextView mTvPayOrderPrice = (TextView) mContentView.findViewById(R.id.tv_pay_order_price);
        TextView mTvOrderStartsCenter=(TextView)mContentView.findViewById(R.id.tv_order_starts_center);
        RelativeLayout mRelPayCoupon1 = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_coupon1);//支付宝支付
        final CheckBox mImgPayOrderOfAlipay = (CheckBox) mContentView.findViewById(R.id.cb_pay_order_of_alipay);
        RelativeLayout mRelPayOrderOfWeixin = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_order_of_weixin);//微信支付
        final CheckBox mImgPayOrderOfWeixin = (CheckBox) mContentView.findViewById(R.id.cb_pay_order_of_weixin);
        TextView mTvPayPayment = (TextView) mContentView.findViewById(R.id.tv_pay_payment);
        TextView mTvPopPayYueTitle = (TextView) mContentView.findViewById(R.id.tv_pop_pay_yue_title);
        RelativeLayout mRelPayOrderOfBalance = (RelativeLayout) mContentView.findViewById(R.id.rel_pay_order_of_balance);//余额支付
        TextView mTvPopPayYuePrice = (TextView) mContentView.findViewById(R.id.tv_pop_pay_yue_price);
        RelativeLayout rel_discount=(RelativeLayout)mContentView.findViewById(R.id.rel_discount);
        TextView tv_discount = (TextView) mContentView.findViewById(R.id.tv_discount);

        DecimalFormat format = new DecimalFormat("#0.00");
        //String str = format.format(disprovn);

        /*for (int i = 0; i < mAddpreOrderResVo.getCustomerVipTips().size(); i++) {
            if (mAddpreOrderResVo.getCustomerVipTips().get(i).isTips()){
                rel_discount.setVisibility(View.VISIBLE);
                //tv_discount.setText("\u00A5"+format.format(mAddpreOrderResVo.getCustomerVipTips().get(i).g));
            }
        }*/

        mTvOrderStartsCenter.setText("支付"+mTitleName);
        if (StateUtils.NORMAL.equals(mStarts)){
            if (mPaybalance >= mDeposit) {
                mTvPayOrderPrice.setText("应付"+mTitleName+":\u00A5 0.00");
                mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - mDeposit) + "元)");
                mTvPayPayment.setText("抵扣支付");
                mTvPopPayYueTitle.setText("余额已抵扣" +format.format(mDeposit) + "元");

            } else {
                mTvPayOrderPrice.setText("应付"+mTitleName+":\u00A5" + format.format(mDeposit - mPaybalance));
                mTvPopPayYuePrice.setText("(剩余:0.00元)");
                mTvPayPayment.setText("确认支付");
                mTvPopPayYueTitle.setText("余额已抵扣" + format.format(mPaybalance) + "元");
            }
            if (mPaybalance >= mDeposit) {
                mAlipayOrWeixin = StateUtils.BALANCE;
                mImgPayOrderOfWeixin.setChecked(false);
                mImgPayOrderOfAlipay.setChecked(false);
            } else {
                mAlipayOrWeixin = StateUtils.WEIXIN;
                mImgPayOrderOfWeixin.setChecked(true);
                mImgPayOrderOfAlipay.setChecked(false);
            }

            //尾款减去所有优惠08.21
            /*if(mPaybalance>= (mTailMoney-mCouponsdiscount-mAssemble-discount-minus)){
                mTvPayOrderPrice.setText("\u00A5 0.00");
                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mTailMoney-mCouponsdiscount-mAssemble-discount-minus)+"元");
                mTvPopPayYuePrice.setText("(剩余:" +format.format(mPaybalance - (mTailMoney-mCouponsdiscount-mAssemble-discount-minus)) + "元)");
            }else {
                mTvPayOrderPrice.setText("\u00A5 "+format.format(mTailMoney-mCouponsdiscount-mAssemble-discount-minus-mPaybalance));
                mTvPopPayYueTitle.setText("余额已抵扣"+format.format(mPaybalance)+"元");
                mTvPopPayYuePrice.setText("(剩余:0.00元)");
            }*/

        }else{
            mTvPayOrderPrice.setText("应付"+mTitleName+":\u00A5" + format.format(mDeposit));
            mTvPopPayYuePrice.setText("");
            mTvPayPayment.setText("确认支付");
            mTvPopPayYueTitle.setText("该账户已被冻结，请联系我们");
            mAlipayOrWeixin = StateUtils.WEIXIN;
            mImgPayOrderOfWeixin.setChecked(true);
            mImgPayOrderOfAlipay.setChecked(false);
        }

        mVwPopBlank.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });

        mTvOrderStartsCancelEvaluate.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });

        mImgPayOrderOfAlipay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StateUtils.NORMAL.equals(mStarts)){
                    if (mPaybalance >= mDeposit) {
                        Toast toast = Toast.makeText(mContext, "余额已抵扣全额", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                        mImgPayOrderOfAlipay.setChecked(false);
                        return;
                    }
                }
                if (mImgPayOrderOfAlipay.isChecked()) {
                    mAlipayOrWeixin = StateUtils.ALIPAY;
                    mImgPayOrderOfWeixin.setChecked(false);
                } else {
                    mAlipayOrWeixin = StateUtils.PAY_NULL;
                }
            }
        });


        mImgPayOrderOfWeixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StateUtils.NORMAL.equals(mStarts)){
                    if (mPaybalance >= mDeposit) {
                        Toast toast = Toast.makeText(mContext, "余额已抵扣全额", Toast.LENGTH_SHORT);
                        ToastUtils.showMyToast(toast, 3000);
                        mImgPayOrderOfWeixin.setChecked(false);
                        return;
                    }
                }

                if (mImgPayOrderOfWeixin.isChecked()) {
                    mAlipayOrWeixin = StateUtils.WEIXIN;
                    mImgPayOrderOfAlipay.setChecked(false);
                } else {
                    mAlipayOrWeixin = StateUtils.PAY_NULL;
                }
            }
        });

        mTvPayPayment.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (StateUtils.PAY_NULL.equals(mAlipayOrWeixin)) {
                    Toast toast = Toast.makeText(mContext, "请选择一种支付方式", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                    return;
                }
                mPoplistener.getPayOrder(mAlipayOrWeixin, mTvPayOrderPrice.getText().toString());
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mPopwindows.dismiss();
                    }
                }, 500);

            }
        });
    }

    public interface PopPayListener {
        void getPayOrder(String PayType, String PayPrice);
    }
}
